package com.tencent.miniproject.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Toast;

import com.tencent.miniproject.R;
import com.tencent.miniproject.base.BaseActivity;
import com.tencent.miniproject.constants.Constant;

import butterknife.BindView;

/**
 * created by aduning 2018.05.27
 */
public class MapActivity extends BaseActivity {

    @BindView(R.id.webview)
    WebView webview;

    @Override
    protected int setLayoutResID() {
        return R.layout.layout_test;
    }

    @Override
    protected void initData() {
        webview.setVerticalScrollbarOverlay(true);
        //设置WebView支持JavaScript
        webview.getSettings().setJavaScriptEnabled(true);

        //在js中调用本地java方法
        webview.addJavascriptInterface(new JsInterface(this), "AndroidWebView");

        //添加客户端支持
        webview.setWebChromeClient(new WebChromeClient());

        //动态id，前端地图需要的参数
        int type = getIntent().getIntExtra("type", Constant.MAP_POST_ID);
        //经纬度
        double lng = getIntent().getDoubleExtra("lng", 0);
        double lat = getIntent().getDoubleExtra("lat", 0);
        if (type == Constant.MAP_POST_ID){
            //动态
            int id = getIntent().getIntExtra("postId", 0);
            webview.loadUrl("http://119.29.35.22/src/individual.html?postId=" + id + "&lng=" + lng + "&lat=" + lat);
        }else if (type == Constant.MAP_KEYWORD){
            String keyword = getIntent().getStringExtra("keyword");
            webview.loadUrl("http://119.29.35.22/src/whole.html?keyword=" + keyword);
        }
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {

    }


    private class JsInterface {
        private Context mContext;

        public JsInterface(Context context) {
            this.mContext = context;
        }

        //在js中调用window.AndroidWebView.showInfoFromJs(name)，便会触发此方法。
        @JavascriptInterface
        public void showInfoFromJs(String name) {
//            Toast.makeText(mContext, name, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent();
            intent.setClass(MapActivity.this, ShowClassifyPostActivity.class);
            intent.putExtra("store_classify", name);
            startActivity(intent);
        }
    }

}
